//
//  NewFaulterCollectionViewController.swift
//  ZYBWeibo
//
//  Created by xthink4 on 16/3/4.
//  Copyright © 2016年 zyb. All rights reserved.
//

import UIKit


class NewFaulterCollectionViewController: UICollectionViewController {

    @IBOutlet weak var layout: UICollectionViewFlowLayout!
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        layout.itemSize=view.bounds.size
        
    }
       let imageCount:Int=4
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell=collectionView.dequeueReusableCellWithReuseIdentifier("NewFaulterCell",forIndexPath: indexPath) as! NewFaulterCell
    
        cell.imageIndex=indexPath.item
    
        return cell
    }
    //cell显示完成调用，
    //注意：参数cell 和indexPath都是之前消失的cell的
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //获取正在现实的额cell
      let path=collectionView.indexPathsForVisibleItems().last! as NSIndexPath
        if path.item==imageCount-1{
            //根据indexPath获取 指定cell
            let cell=collectionView.cellForItemAtIndexPath(path)as!NewFaulterCell
            cell.playStartBtn()
        }
        
    }

   }
class NewFaulterCell: UICollectionViewCell {
    @IBOutlet weak var newFaulterImage: UIImageView!
    
   
    @IBOutlet weak var startBtn: UIButton!
    var imageIndex:Int=0{
        didSet{
           newFaulterImage.image=UIImage(named: "new_feature_\(imageIndex+1)")
            startBtn.hidden=true
            //这里播放动画不合适
//            if imageIndex==3
//            {
//                startBtn.hidden=false
//                startBtn.transform=CGAffineTransformMakeScale(0, 0)
//                UIView.animateWithDuration(2.0, animations: { () -> Void in
//                    self.startBtn.transform=CGAffineTransformMakeScale(1.0, 1.0)
//                    }, completion: { (_) -> Void in
//                        print("ok")
//                })
//            }
        }
    }
    //启动动画
    func playStartBtn(){
        startBtn.hidden=false
        startBtn.transform=CGAffineTransformMakeScale(0, 0)
//        UIView.animateWithDuration(1.2, animations: { () -> Void in
//            self.startBtn.transform=CGAffineTransformMakeScale(1.0, 1.0)
//            }, completion: { (_) -> Void in
//                print("ok")
//        })
        ///delay  延迟时间  usingSpringWithDamping 弹性系数  initialSpringVelocity 速度
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.4, initialSpringVelocity: 5.0, options:UIViewAnimationOptions.TransitionNone, animations: { () -> Void in
            self.startBtn.transform=CGAffineTransformMakeScale(1.0, 1.0)
            }) { (_) -> Void in
                
        }
   }
    @IBAction func startBtn(sender: AnyObject) {
        
        print(__FUNCTION__)
         NSNotificationCenter.defaultCenter().postNotificationName(YBSwithRootViewControllerNSNotification, object: "Main")
    }
}
